local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());

local ShieldPerfab =CommonOHOH:GetPath_Loop("Resource.Effect.Shield",game.ReplicatedStorage)


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.player=self.parent.player
    self.player.BattleSateEvent:Connect(function(isEnter)
        if isEnter then
            self:Show()
        else
            self:Hide()
        end
    end)

end;

function M.dtor(self)
	M.super.dtor(self);
end


-- 圆的半径
local gRadius = 17
local gMinAngle = -80
local gMaxAngle = 80

function M.GetFollowPos(self)
    local characterCFrame=self.parent:GetCFrame()
    -- 获取角色的朝向方向
    local forwardVector = characterCFrame.LookVector
    -- 生成一个[-30度, 30度]范围内的随机角度，并将其转换为弧度
    local randomAngle = math.rad(math.random(gMinAngle, gMaxAngle))
    -- 计算对应的方向
    local offsetVector = Vector3.new(
        forwardVector.X * math.cos(randomAngle) - forwardVector.Z * math.sin(randomAngle),
        forwardVector.Y,
        forwardVector.X * math.sin(randomAngle) + forwardVector.Z * math.cos(randomAngle)
    ).unit

    -- 计算圆弧上的点
    local pointOnArc = characterCFrame.Position + offsetVector *gRadius
    return pointOnArc
end


function M.IsOutRange(self,pos)
    local nowPos=self.parent:GetPosition()
    return (nowPos-pos).Magnitude>1.5*gRadius
end


function M.Show(self)

    -- self.Shield=ShieldPerfab:Clone()


    -- self.Shield.Parent=self.parent.main_inst
    -- local startPos= self.Shield:WaitForChild("StartPos")
    -- local hurt= self.Shield:WaitForChild("hurt")

    -- local pos=self.parent:GetCFrame():PointToWorldSpace(startPos.Value)
    -- self.Shield.Position=pos
    -- hurt.Position=pos
    -- local root=self.parent:GetRootPart_Check()
    -- local w=Instance.new("WeldConstraint",hurt)
    -- w.Part0=hurt
    -- w.Part1=root
    -- local attVal= self.Shield:WaitForChild("CharacterCF")
    -- self.HingeConstraintAtt=Instance.new("Attachment",root)
    -- self.HingeConstraintAtt.CFrame=attVal.Value
    -- self.HingeConstraintAtt.Name="Shield"

    -- local hingeConstraint= self.Shield:WaitForChild("HingeConstraint")
    -- hingeConstraint.Attachment1= self.HingeConstraintAtt

end


function M.Hide(self)
	-- if self.Shield then
    --     self.Shield:Destroy()
    --     self.HingeConstraintAtt:Destroy()
    -- end
end


return M

